package com.example.onlinetaxi.util;

import com.example.onlinetaxi.LocalStroage.LocalStorage;
import com.example.onlinetaxi.dto.OrderDto.CompletedOrderDto;
import com.example.onlinetaxi.dto.OrderDto.ReceivedOrderDto;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TimeUtil {
    public static String DateToString(Date date) {
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return ft.format(date);
    }

    public static Date StringToDate(String dateString) {
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        try {
            return ft.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static long getDateDifference(Date createDate, Date currentDate) {
        long diff = currentDate.getTime() - createDate.getTime();//这样得到的差值是毫秒级别

        // 用户给出的两个时间存在问题
        if (diff < 0) {
            return -1;
        }

        long seconds = diff / 1000;
        return seconds;
    }

    public static Date TimeStampToDate(long timeStamp) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 时间戳转换成时间
        String sd = sdf.format(new Date(Long.parseLong(String.valueOf(timeStamp))));
        // 将时间转化为Date类型
        return StringToDate(sd);
    }

    public static CompletedOrderDto setRightTimeFormat(CompletedOrderDto orderDto) {
        if (orderDto.getCreateTime() != null) {
            orderDto.setCreateTime(TimeUtil.TimeStampToDate(orderDto.getCreateTime().getTime()));
        }
        if (orderDto.getUseCarTime() != null) {
            orderDto.setUseCarTime(TimeUtil.TimeStampToDate(orderDto.getUseCarTime().getTime()));
        }


        return orderDto;
    }
}
